/*
 * Tag
 */

import Tag from 'constants/tag';
import { CALL_BOOK_API } from '../middlewares';

/**
 * 创建学期记录
 * @param data TagModal
 */
export function createTag(data: Record<string, any>) {
  return {
    data,
    [CALL_BOOK_API]: {
      types: [Tag.CREATE, Tag.CREATE_SUCCESS, Tag.CREATE_FAIL],
      uri: '/course-api/a/tags',
      method: 'POST',
      body: data,
    },
  };
}

/**
 * 更新标签记录
 * @param data TagModal
 */
export function updateTag(tagId: number, data: Record<string, any>) {
  return {
    data,
    [CALL_BOOK_API]: {
      types: [Tag.UPDATE, Tag.UPDATE_SUCCESS, Tag.UPDATE_FAIL],
      uri: `/course-api/a/tags/${tagId}`,
      method: 'PUT',
      body: data,
    },
  };
}

/**
 * 删除标签
 * @param tagId
 */
export function removeTag(tagId: number) {
  return {
    tagId,
    [CALL_BOOK_API]: {
      types: [Tag.REMOVE, Tag.REMOVE_SUCCESS, Tag.REMOVE_FAIL],
      uri: `/course-api/a/tags/${tagId}`,
      method: 'DELETE',
    },
  };
}

/**
 * 查询Tag列表
 * @param filters Tag查询过滤
 */
export function fetchTagList(filters: Record<string, any> = {}) {
  return {
    filters,
    [CALL_BOOK_API]: {
      types: [Tag.FETCH_LIST, Tag.FETCH_LIST_SUCCESS, Tag.FETCH_LIST_FAIL],
      uri: '/course-api/a/tags',
      method: 'GET',
      body: filters,
    },
  };
}

/**
 * Tag.checkable 选中状态
 * @param tag
 * @param checked
 * @param max 最大选择数量
 */
export function toggleTagCheck(tag: any, checked: boolean, max: number = 0) {
  return {
    type: Tag.TOGGLE_CHECK,
    checked,
    tag,
    max,
  };
}

export function initSelectedItems(ownItems: any[]) {
  return {
    type: Tag.INIT_SELECTED_ITEM,
    ownItems,
  };
}
